/*
 * HomeShopHolder.java
 * business
 *
 * Created by SunDing on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.listview.viewholder;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.data.bean.Goods;
import com.yidiandao.business.data.bean.Shop;
import com.yidiandao.business.ui.widget.listview.ViewHolder;
import com.yidiandao.business.ui.widget.view.RatioLayout;
import com.yidiandao.business.utils.ImageLoader;
import com.yidiandao.business.utils.PriceUtils;
import com.yidiandao.business.utils.UIUtils;

import java.util.List;

/** 首页-商店 Item 的ViewHolder */
public class ShopHolder extends ViewHolder {

    public TextView mTvGoToShop;
    public TextView mTvPrice1;
    public TextView mTvPrice2;
    public TextView mTvPrice3;
    public ImageView mIvImage1;
    public ImageView mIvImage2;
    public ImageView mIvImage3;
    private ImageView mIvShopImage;
    private TextView mTvShopName;
    private TextView mTvShopDetail;
    private TextView mTvQuality;
    private TextView mTvService;
    private TextView mTvLogistics;
    private LinearLayout mLlytImageContainer;

    private RatioLayout mRatioLayout1;
    private RatioLayout mRatioLayout2;
    private RatioLayout mRatioLayout3;

    @Override
    protected void initView(View rootView) {

        mIvShopImage = (ImageView) rootView.findViewById(R.id.iv_shop_info_image);
        mTvShopName = (TextView) rootView.findViewById(R.id.tv_shop_name);
        mTvShopDetail = (TextView) rootView.findViewById(R.id.tv_shop_detail);
        mTvGoToShop = (TextView) rootView.findViewById(R.id.tv_go_to_shop);
        mTvQuality = (TextView) rootView.findViewById(R.id.tv_quality);
        mTvService = (TextView) rootView.findViewById(R.id.tv_service);
        mTvLogistics = (TextView) rootView.findViewById(R.id.tv_logistics);
        mIvImage1 = (ImageView) rootView.findViewById(R.id.iv_image_1);
        mTvPrice1 = (TextView) rootView.findViewById(R.id.tv_price_1);
        mIvImage2 = (ImageView) rootView.findViewById(R.id.iv_image_2);
        mTvPrice2 = (TextView) rootView.findViewById(R.id.tv_price_2);
        mIvImage3 = (ImageView) rootView.findViewById(R.id.iv_image_3);
        mTvPrice3 = (TextView) rootView.findViewById(R.id.tv_price_3);

        mLlytImageContainer = (LinearLayout) rootView.findViewById(R.id.llyt_image_container);
        mRatioLayout1 = (RatioLayout) rootView.findViewById(R.id.ratio_layout_1);
        mRatioLayout2 = (RatioLayout) rootView.findViewById(R.id.ratio_layout_2);
        mRatioLayout3 = (RatioLayout) rootView.findViewById(R.id.ratio_layout_3);
    }

    @Override
    protected int getItemLayoutId() {
        return R.layout.item_home_shop;
    }

    public void refreshUI(Shop shop) {

        List<Goods> goodsInfo = shop.getGoodsInfo();

        ImageLoader.loadShopLogo(mContext, mIvShopImage, shop.getShopLogo());
        mTvShopName.setText(shop.getShopName());

        // 商品种类
        int goodsNumber = shop.getGoodsNumber();
        // 商品销量
        int saleNumber = shop.getSelesNum();
        mTvShopDetail.setText(
                UIUtils.getString(R.string.shop_sales_count, goodsNumber));

        // 酒品描述
        float goodsDesc = shop.getCommentRand();
        PriceUtils.setShopValueChange(mTvQuality, goodsDesc);
        // 商家服务
        float server = shop.getServer();
        PriceUtils.setShopValueChange(mTvService, server);
        // 物流描述
        float shipping = shop.getShipping();
        PriceUtils.setShopValueChange(mTvLogistics, shipping);
        try {
            if (goodsInfo == null || goodsInfo.size() == 0) {
                // 没有图片，直接返回
                mLlytImageContainer.setVisibility(View.GONE);
                return;
            }

            mLlytImageContainer.setVisibility(View.VISIBLE);

            mRatioLayout2.setVisibility(View.INVISIBLE);
            mRatioLayout3.setVisibility(View.INVISIBLE);

            // 第一个商品
            Goods goods1 = goodsInfo.get(0);
            ImageLoader.loadImageWithPlaceHolder(mContext, mIvImage1, goods1.getGoodsThumb());
            mTvPrice1.setText(goods1.getShopPrice());

            // 第二个商品
            Goods goods2 = goodsInfo.get(1);
            mRatioLayout2.setVisibility(View.VISIBLE);
            ImageLoader.loadImageWithPlaceHolder(mContext, mIvImage2, goods2.getGoodsThumb());
            mTvPrice2.setText(goods2.getShopPrice());

            // 第三个商品
            Goods goods3 = goodsInfo.get(2);
            mRatioLayout3.setVisibility(View.VISIBLE);
            ImageLoader.loadImageWithPlaceHolder(mContext, mIvImage3, goods3.getGoodsThumb());
            mTvPrice3.setText(goods3.getShopPrice());

        } catch (IndexOutOfBoundsException e) {
        }
    }
}
